<template>
  <a-modal :title="title" :visible="open" :confirm-loading="submitLoading" @ok="handleSubmit" @cancel="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="角色名称" prop="roleName">
        <a-input v-model="form.roleName" placeholder="请输入角色名称" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="角色标识" prop="roleKey">
        <a-input v-model="form.roleKey" placeholder="请输入角色标识" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="角色描述" prop="description">
        <a-input v-model="form.description" placeholder="请输入角色描述" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="角色权限" prop="permissionIds">
        <a-tree
          v-model="permissionIds"
          checkable
          :tree-data="treeList"
          :replaceFields="{children:'children', title: 'menuName', key:'id', value: 'id'}"/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import {getSysRole, saveSysRole, updateSysRole} from '@/api/system/sysRole'
import {listSysPermission} from '@/api/system/sysPermission'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        roleName: [{required: true, trigger: ['blur', 'change'], message: '请输入角色名称'}],
        roleKey: [{required: true, trigger: ['blur', 'change'], message: '请输入角色标识'}],
        description: [{required: true, trigger: ['blur', 'change'], message: '请输入角色描述'}]
      },
      treeList: [],
      permissionIds: []
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        roleName: undefined,
        roleKey: undefined,
        description: undefined,
        permissionIds: undefined
      }
      this.permissionIds = []
      listSysPermission().then(res => {
        this.treeList = this.handleTree(res.data)
      })
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加角色'
      this.open = true
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getSysRole(row.id).then(res => {
        this.title = '修改角色'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
          if (this.form.permissionIds) {
            this.permissionIds = this.form.permissionIds.split(',').map(id => Number(id))
          }
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          this.form.permissionIds = this.permissionIds.join(',')
          if (this.form.id && this.form.id > 0) {
            updateSysRole(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveSysRole(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
